#!/usr/bin/env bash

LOG_FILE="refresh.log"
DATA_FILE="dl_addr.txt"

WGET="wget"
SUDO="sudo"
#REPO_DIR="./pkgs"
REPO_DIR="/var/cache/pacman/pkg"
APPNAME=$(basename "${0}")


error () { echo "XXX $@";}
warn () { echo "!!! $@";}
info () { echo "*** $@";}

GET_LOG="n" 
CREATE="n" 

usage ()
{
    echo " 用法:  ${APPNAME} [选项]  命令 <命令选项>"
    echo " 选项:"
    echo "    -c               仅仅是通过pacman -Sy取得log信息"
    echo "    -r               设置仓库地址, 默认为 $REPO_DIR"
    echo "    -h               本帮助信息."
    echo " 命令:"
    echo "     create  创建解析文本"
    echo "     show  显示信息"
    echo "     download 下载仓库文件"
    echo "示例:"
    echo "  -c -r ./pkgs  create  创建解析文本"
    echo "  -r ./pkgs   show  显示信息"
    echo "  -r ./pkgs   download 下载仓库文件"
    exit $1
}


function _get_log() {
    echo "" > $1
    $SUDO pacman -Sy 2>&1 | tee $1
    echo "get_log Success!!"
}

function _check_file() {
    if [ ! -e $1 ] ; then
        echo "not found $1"
        exit -1
    fi
}

function _parse_log() {
    if [ $# -ne 2 ] ; then 
        echo "error,   use  function = parse_log out.log  save.log"
        return -1
    fi
    grep "ftp://"  $1 | awk '{print $3 }' > $2
    grep "http://" $1 | awk '{print $3 }' >> $2
    echo "parse_log Success!!"
    return 0
}
function _create() {
    if [ $GET_LOG = "y" ] ; then
        _get_log $LOG_FILE
    fi
    _check_file $LOG_FILE
    _parse_log $LOG_FILE $DATA_FILE
}

function _show() {
    echo "_show()"
    _check_file $DATA_FILE
    cat $DATA_FILE | while read dl_addr; do
        repo_db_file=$(awk 'BEGIN { FS="/" } {print $NF}' <<< $dl_addr)
        TARGET_FILE=${REPO_DIR}/${repo_db_file} 
        echo "$repo_db_file ==>> $dl_addr  --> ${TARGET_FILE}"
        if [ -e ${TARGET_FILE} ] ; then
            echo "${TARGET_FILE} is exist!!!"
        else
            echo "${TARGET_FILE} not found!!!"
        fi
    done
}

function _download() {
    echo "_download()"
    _check_file $DATA_FILE
    cat $DATA_FILE | while read dl_addr; do
        repo_db_file=$(awk 'BEGIN { FS="/" } {print $NF}' <<< $dl_addr)
        TARGET_FILE=${REPO_DIR}/${repo_db_file} 
        echo "$repo_db_file ==>> $dl_addr  --> ${TARGET_FILE}"
        if [ -e ${TARGET_FILE} ] ; then
            echo "${TARGET_FILE} is exist!!! rm ....."
            $SUDO rm -f ${TARGET_FILE}
        else
            echo "${TARGET_FILE} not found!!!"
        fi
        $SUDO  $WGET $dl_addr -O ${TARGET_FILE}
        dl_stat=$?
        if [ $dl_stat -eq 0 ] ; then
            echo "Download $dl_addr Success!"
        else
            echo "Download $dl_addr faile!!!!!"
        fi
    done
}

# 检测、配置输入的命令
while getopts 'cr:fh' arg; do
    case "${arg}" in
	c) CREATE="y";GET_LOG="y" ; echo "get log" ;;
    r) REPO_DIR="${OPTARG}"; echo "REPO_DIR = $REPO_DIR" ;;
	#g) GET_LOG="y" ; echo "get log";;
	f) FORCE="y" ;;
    h|?) usage 0 ;;
    *) echo "无效参数 '${arg}'"; usage 1 ;;
    esac
done

echo "参数及命令: $@"
shift $(($OPTIND - 1))
echo "命令: $@"
#[ $# -le 1 ] && usage 1

command_name="${1}"

#case "${command_name}" in
       #create) REPO_DIR="${2}"; REPO_NAME="${3}"; remark="创建本地自定义软件仓(Repo)" ;;
#       upload) REPO_DIR="${2}"; Project_Name="${3}"; remark="将本地自定义软件仓(Repo)提交到Googlecode" ;;
#       add) REPO_DIR="${2}"; REPO_NAME="${3}"; remark="将本地自定义软件仓(Repo)加到软件仓配置文件${PACMANCONF}" ;;
#       c_and_u) REPO_DIR="${2}"; REPO_NAME="${3}"; Project_Name="${4}"; remark="创建Repo并上传到${Project_Name}项目" ;;
#       c_and_a) REPO_DIR="${2}"; REPO_NAME="${3}"; remark="创建Repo并加到软件仓配置文件${PACMANCONF}" ;;
#       *) warn "未知命令'${command_name}'"; usage 1 ;;
#esac
#

echo "CREATE=$CREATE"
echo "GET_LOG=$GET_LOG"
echo "REPO_DIR = $REPO_DIR" 

if [ "${command_name}" = "create" -o "${command_name}" = "all" ]; then 
    echo "command_name = $command_name start _create()"
    _create 
fi
if [ "${command_name}" = "show" -o "${command_name}" = "all" ]; then 
    echo "command_name = $command_name start _show()"
    _show
fi

if [ "${command_name}" = "download" -o "${command_name}" = "all" ]; then 
    echo "command_name = $command_name start _download()"
    _download
fi

#===============================================================================
# demo add to ~/.bash_profile
# run update_repo to update repo
#   function update_repo() {
#       SDIR="${HOME}/myhome/pkgbuild/mypkg_scripts"
#       SFILE="mypkg_parse.sh"
#       if [ -d $SDIR ] ; then
#       cd $SDIR
#           if [ -x $SFILE ] ;then
#             ./${SFILE} download
#         else 
#             echo "error: $SFILE can't run!!!"
#           fi
#       else
#           echo "error:  $SDIR dir no exists!!!"
#       fi
#   }
#   #===============================================================================
